package com.lsec.springbootinit.security.aop;


import com.lsec.springbootinit.exception.enums.TypicalCode;
import com.lsec.springbootinit.security.annotations.PreAuthenticated;
import com.lsec.springbootinit.security.utils.SecurityFrameworkUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import static com.lsec.springbootinit.model.common.ServiceException.exception;


/**
 * 切面
 *
 * @author Cherry
 * @date 2023/06/12
 */
@Aspect
@Slf4j
public class PreAuthenticatedAspect {

    @Around("@annotation(preAuthenticated)")
    public Object around(ProceedingJoinPoint joinPoint, PreAuthenticated preAuthenticated) throws Throwable {
        if (SecurityFrameworkUtils.getLoginUser() == null) {
            throw exception(TypicalCode.ACCESS_DENIED);
        }
        return joinPoint.proceed();
    }

}
